<template>
  <div class="comment-page">
    <el-page-header @back="$router.back()" content="评论管理" class="header">
    </el-page-header>
    <el-table :data="articles" style="width: 100%" class="data-table">
      <el-table-column label="标题" prop="title"> </el-table-column>
      <el-table-column label="总评论数" prop="total_comment_count">
      </el-table-column>
      <el-table-column label="粉丝评论数" prop="fans_comment_count">
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          {{ scope.row.comment_status ? "正常" : "关闭" }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.comment_status"
            active-color="#ff4949"
            inactive-color="#13ce66"
            @change="onStatusChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="totalCount"
      @current-change="onCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import { GetArticle, UpdataCommentStatus } from "@/services/article";

export default {
  name: "",
  data() {
    return {
      articles: [],
      totalCount: 0,
    };
  },
  created() {
    this.loadArticle(1);
  },
  methods: {
    loadArticle(page = 1) {
      GetArticle({
        response_type: "comment",
        page,
        per_page: 10
      }).then((res) => {
        // console.log(res);
        this.articles = res.data.data.results;
        this.totalCount = res.data.data.total_count;
      });
    },
    onStatusChange(article) {
      UpdataCommentStatus(article.id, article.comment_status).then((res) => {
        console.log(res);
        this.$message({
          type: "success",
          message: article.comment_status ? "评论开启" : "评论关闭",
        });
      });
    },
    onCurrentChange(page) {
      this.loadArticle(page);
    },
  },
};
</script>

<style scoped>
.comment-page {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin-bottom: 25px;
}

.header {
  border-bottom: 1px solid #eee;
  padding-bottom: 20px;
}

.data-table {
  margin-bottom: 20px;
}
</style>
